package com.mcz.yun4book.service.impl;

import com.mcz.yun4book.dao.mapper.BookMapper;
import com.mcz.yun4book.entity.Book;
import com.mcz.yun4book.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.awt.print.Pageable;

@Service
public class BookServicelmpl implements BookService {
    @Autowired
    private BookMapper bookMapper;
    @Override
    public Page<Book> findAllBook(int page, int limit, Book book) {
        if (StringUtils.isEmpty(book.getName())){
            book.setName("");
        }
        if (StringUtils.isEmpty(book.getPub())){
            book.setPub("");
        }
        return (Page<Book>) bookMapper.findByNameContainingAndPubContainingOrderByIdDesc(book.getName(), book.getPub(), PageRequest.of(page-1,limit));

    }

    @Override
    public int delBookById(int id) {

                try{bookMapper.deleteById(id);}
                catch (Exception e){
                    return  0;
                }
                return 1;
    }
    @Override
    public Book addBook(Book book){
        return bookMapper.save(book);
    }
    @Override
    public Book findBookId(int id){
        return bookMapper.findById(id).get();
    }

    @Override
    public Book updateBook(Book book) {
        return bookMapper.save(book);
    }


}
